package com.longrise.android.byjk.plugins.hra.hratabthird;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.Html;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.plugins.hra.HraSearchActivity;
import com.longrise.android.byjk.plugins.hra.SearchBoxView;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.PrintLog;

/**
 * Created by Administrator on 2018/1/12.
 * 我的改签搜索结果界面
 */

public class EndorseSearchActivity extends BaseActivity2<MineEndorsePresenter> implements MineEndorseAdapter.OnItemClickListener, MineEndorseContract.View, SwipeRefreshLayout.OnRefreshListener, TextWatcher, View.OnFocusChangeListener, View.OnClickListener {

    public static final String TAG = "EndorseSearchActivity";
    public static final int ENDORSESEARCH = 1;
    private BBswipeRefreshLayout mSrl;
    private RecyclerView mRcv;
    private MineEndorseAdapter mAdapter;
    private String searchText;
    private SearchBoxView mSearchToolbar;
    private EntityBean[] mHraListBean;
    private ImageView iv_order_search_back;
    private TextView tv_hra_search;
    private LinearLayout endorse_search_hint;
    private LinearLayout endorse_search_notice;
    private TextView tv_hra_search_result;
    private LinearLayout ll_endorse_null;


    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_endorse_search;
    }

    @Override
    public void initView() {
        mSrl = (BBswipeRefreshLayout) findViewById(R.id.endorse_search_sp);
        mRcv = (RecyclerView) findViewById(R.id.endorse_search_rv);
        mSearchToolbar = (SearchBoxView) findViewById(R.id.search_toolbar);
        iv_order_search_back = (ImageView) findViewById(R.id.iv_order_search_back);  //返回按钮
        tv_hra_search = (TextView) findViewById(R.id.tv_hra_search);  //搜索按钮
        endorse_search_hint = (LinearLayout) findViewById(R.id.endorse_search_hint);
        endorse_search_notice = (LinearLayout) findViewById(R.id.endorse_search_notice);
        tv_hra_search_result = (TextView) findViewById(R.id.tv_hra_search_result);
        ll_endorse_null = (LinearLayout) findViewById(R.id.layout_endorse_search_null);

        setToolbarVisible(false);

        getExtra();
        initRcv();
        initEvent();
        initNameEt(mSearchToolbar.getEditText());
        if(mSearchToolbar.getEditText().isFocusable()){
            hideSoftKey(mSearchToolbar.getEditText());
        }
        mPresenter.getSearchEndorse(Control.getEndorseName());
    }

    public void getExtra(){
        Intent intent = getIntent();
        searchText = intent.getStringExtra(HraSearchActivity.HRASEARCH);
        Control.setEndorseName(searchText);
        mSearchToolbar.setText(Control.getEndorseName());
        mSearchToolbar.setRightClickMode(SearchBoxView.USERIF);
    }


    private void initRcv() {
        mSrl.setOnRefreshListener(this);
        mAdapter = new MineEndorseAdapter();
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRcv.setLayoutManager(manager);
        mRcv.setAdapter(mAdapter);
    }

    private void initEvent() {
        mAdapter.setOnItemClickListener(this);

        iv_order_search_back.setOnClickListener(this);
        tv_hra_search.setOnClickListener(this);
        mSearchToolbar.getEditText().setOnFocusChangeListener(this);
        mSearchToolbar.getEditText().addTextChangedListener(this);
        endorse_search_hint.setOnClickListener(this);
    }


    @Override
    public void onClick(EntityBean item) {
        jump2HraEndorse(item);
    }

    //跳转到预约改签界面
    public void jump2HraEndorse(EntityBean item) {
        PrintLog.e(TAG,"item=:"+item);
        Intent intent = new Intent(mContext, HraOrderEndorseActivity.class);
        String entityBean = JSONSerializer.getInstance().Serialize(item);
        intent.putExtra(HraOrderEndorseActivity.ENDORSESEARCHBEAN,entityBean);
        intent.setFlags(ENDORSESEARCH);
        startActivity(intent);
    }

    @Override
    public void onToolbarBackClick() {

    }

    public void hideSoftKey(EditText editText){
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0) ;
        }
    }

    @Override
    public void refreshData(EntityBean bean) {
        mHraListBean = bean.getBeans("result");
        if(mHraListBean != null){
            mAdapter.refreshData(mHraListBean);
            mSrl.setVisibility(View.VISIBLE);
            ll_endorse_null.setVisibility(View.GONE);
        }else {
            ll_endorse_null.setVisibility(View.VISIBLE);
            mSrl.setVisibility(View.GONE);
        }

    }

    @Override
    public void setRefreshing(boolean isRefreshing) {
        mSrl.setRefreshing(isRefreshing);
    }

    @Override
    public void onRefresh() {
        mPresenter.getSearchEndorse(Control.getEndorseName());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //点击提示框
            case R.id.endorse_search_hint:
                mSrl.setVisibility(View.VISIBLE);
                endorse_search_notice.setVisibility(View.VISIBLE);
                endorse_search_hint.setVisibility(View.GONE);
                if(mSearchToolbar.getEditText().isFocusable()){
                    mSearchToolbar.getEditText().clearFocus();
                    mSearchToolbar.getEditText().setFocusableInTouchMode(true);
                    hideSoftKey(mSearchToolbar.getEditText());
                    PrintLog.e(TAG,"mSearchToolbar"+mSearchToolbar.getEditText().isFocusable());
                }
                mPresenter.getSearchEndorse(Control.getEndorseName());
                break;
            //点击返回按钮
            case R.id.iv_order_search_back:
                finish();
                break;
            //点击搜索按钮
            case R.id.tv_hra_search:

                mSrl.setVisibility(View.VISIBLE);
                endorse_search_notice.setVisibility(View.VISIBLE);
                endorse_search_hint.setVisibility(View.GONE);

                if(mSearchToolbar.getEditText().isFocusable()){
                    mSearchToolbar.getEditText().clearFocus();
                    mSearchToolbar.getEditText().setFocusableInTouchMode(true);
                    hideSoftKey(mSearchToolbar.getEditText());
                    PrintLog.e(TAG,"mSearchToolbar"+mSearchToolbar.getEditText().isFocusable());
                }
                mPresenter.getSearchEndorse(Control.getEndorseName());
                break;

        }
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String text = mSearchToolbar.getText().toString().trim();
        if (!TextUtils.isEmpty(text)) {
            tv_hra_search.setEnabled(true);
            endorse_search_hint.setVisibility(View.VISIBLE);
            Control.setEndorseName(s.toString());
            tv_hra_search_result.setText(Html.fromHtml("搜索 " + "&#34"+s + "&#34" + " 的HRA预约"));
        } else {
            tv_hra_search.setEnabled(false);
            endorse_search_hint.setVisibility(View.GONE);
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        String username = mSearchToolbar.getText();
        if(!hasFocus){ //失去焦点
            mSearchToolbar.setRightIcVisible(false);
            mSrl.setVisibility(View.VISIBLE);
            endorse_search_notice.setVisibility(View.VISIBLE);
            endorse_search_hint.setVisibility(View.GONE);
        }else{
            if(!TextUtils.isEmpty(username)) {
                mSrl.setVisibility(View.GONE);
                endorse_search_notice.setVisibility(View.GONE);
                endorse_search_hint.setVisibility(View.VISIBLE);
                ll_endorse_null.setVisibility(View.GONE);
                mSearchToolbar.setRightIcVisible(true);
                Control.setEndorseName(username);
                tv_hra_search_result.setText(Html.fromHtml("搜索 " + "&#34"+ username + "&#34" + " 的HRA预约"));
            }
        }
    }

    /**
     * 姓名输入框只能输入汉字 并且少于15个汉字
     */
    private void initNameEt(EditText et_name) {
        et_name.setInputType(InputType.TYPE_CLASS_TEXT);
        et_name.setFilters(new InputFilter[]{
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                                               int dstart, int dend) {
                        String dests = dest.toString(); //已经显示的字符串
                        int destslen = 0;
                        if (!TextUtils.isEmpty(dests)) {
                            destslen = dests.length();
                        }
                        String s = dest.toString() + source;
                        if (destslen >= 15) {  //已经显示的字符串长度如果大于15 则不能输入
                            return "";
                        } else {
                            if ((s.length() <= 15) && (source.toString().matches("[\u4e00-\u9fa5]+"))) { //要输入的字符串长度+已经显示的字符串长度<=15
                                return source;
                            } else {
                                if ((!TextUtils.isEmpty(source)) &&
                                        (source.toString().matches("[\u4e00-\u9fa5]+"))) {
                                    String inputs = source.toString().substring(0, 15 - destslen);
                                    return inputs;
                                }
                            }
                        }
                        return "";
                    }
                }
        });

    }
}
